package org.platform.lwc.web.error;


import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.platform.lwc.tool.api.R;
import org.platform.lwc.tool.api.ResultCode;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理，处理可预见的异常
 * 处理error请求 @ControllerAdvice用来处理系统中的Exception，ErrorController处理系统中的Error
 *
 * @author lwc
 * @date 2020/12/28
 */
@Slf4j
@Configuration
//@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@RestControllerAdvice
public class RestExceptionTranslator {



	@ExceptionHandler(Throwable.class)
	@ResponseStatus(HttpStatus.OK)
	public R handleError(Throwable e) {
		log.error("服务器异常", e);
		return R.fail(ResultCode.INTERNAL_SERVER_ERROR, (StrUtil.isEmpty(e.getMessage()) ? ResultCode.INTERNAL_SERVER_ERROR.getMessage() : e.getMessage()));
	}

}
